package company.tx;

import java.util.Scanner;

public class Tx3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println(getMax(sc.nextLong()));
        }
    }

    private static long getMax(long n) {
        String s = String.valueOf(n);
        int length = s.length();
        StringBuffer sb = new StringBuffer();
        long ans = 0;
        for (int i = 0; i < length - 1; i++) {
            sb.append(9);
            ans += 9;
        }
        if (sb.length() > 0) {
            long l = n - Long.parseLong(sb.toString());
            String s1 = String.valueOf(l);
            int length1 = s1.length();
            for (int i = 0; i < length1; i++) {
                ans += (s1.charAt(i) - '0');
            }
            return ans;
        } else
            return n;
    }
}
